﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using TheServer.Controllers;

namespace TheServer.Models
{
    public class AccountDAO
    {
        public static int SignUp(string signup, AccountDTO acc)
        {
            try
            {
                List<Account> lstAcc = MyDatabase.db.Accounts.ToList();
                for (int i = 0; i < lstAcc.Count; i++)
                {
                    if (lstAcc[i].TaiKhoan == acc.TaiKhoan)
                        return 2;
                }
                Account _acc = new Account();
                _acc.TaiKhoan = acc.TaiKhoan.ToLower();
                _acc.MatKhau = acc.MatKhau;
                _acc.TinhTrang = false;
                _acc.OpenID = false;
                MyDatabase.db.AddToAccounts(_acc);
                MyDatabase.db.SaveChanges();
                HomeController.currentImageOwner = acc.TaiKhoan;
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public static int SignUpOpenID(string signup, AccountDTO acc)
        {
            try
            {
                List<Account> lstAcc = MyDatabase.db.Accounts.ToList();
                for (int i = 0; i < lstAcc.Count; i++)
                {
                    if (lstAcc[i].TaiKhoan == acc.TaiKhoan)
                        return 2;
                }
                Account _acc = new Account();
                _acc.TaiKhoan = acc.TaiKhoan.ToLower();
                _acc.MatKhau = acc.MatKhau;
                _acc.TinhTrang = false;
                _acc.OpenID = true;
                MyDatabase.db.AddToAccounts(_acc);
                MyDatabase.db.SaveChanges();
                HomeController.currentImageOwner = acc.TaiKhoan;
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public static int LogOut(AccountDTO taikhoanThoat)
        {
            try
            {
                var _acc = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == taikhoanThoat.TaiKhoan);
                _acc.TinhTrang = false;
                MyDatabase.db.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        public static int PostHinhAnh(string hinhanh, Byte[] StreamImg)
        {
            try
            {
                var ms = new MemoryStream(StreamImg);
                ms.Write(StreamImg, 0, StreamImg.Length);
                Image img = Image.FromStream(ms);
                img.Save(HostingEnvironment.ApplicationPhysicalPath + @"\HinhAnh\" + hinhanh + ".jpg");

                return 1;
            }
            catch (IOException)
            {
                //throw new HttpResponseException(
                //    HttpStatusCode.InternalServerError);
                return -1;
            }
        }
    }
}